<?php
/**
 *	Logic for Messages.
 *	@category		cmTools
 *	@package		BugTracker.Logic
 *	@author			Christian Würker <christian.wuerker@ceus-media.de>
 *	@since			0.1
 *	@version		$Id$
 */
/**
 *	Logic for Messages.
 *	@category		cmTools
 *	@package		BugTracker.Logic
 *	@extends		CMF_Krypton_Core_Logic
 *	@uses			Model_Message
 *	@uses			Logic_User
 *	@author			Christian Würker <christian.wuerker@ceus-media.de>
 *	@since			0.1
 *	@version		$Id$
 */
class Logic_Message extends CMF_Krypton_Core_Logic
{
	public static function getMessage( $messageId )
	{
		$model	= new Model_Message( $messageId );
		$data	= $model->get( TRUE );
		if( !$data )
			throw new LogicException( 'message_id_invalid' );
		return $data;
	}

	public static function getMessagesReceivedBy( $userId, $orders = array(), $limits = array() )
	{
		$conditions	= array( 'target' => $userId );
		return self::getMessages( $conditions, $orders, $limits );
	}

	public static function getMessagesSentBy( $userId, $orders = array(), $limits = array() )
	{
		$conditions	= array( 'source' => $userId );
		return self::getMessages( $conditions, $orders, $limits );
	}

	public static function getMessages( $conditions = array(), $orders = array(), $limits = array() )
	{
		$model	= new Model_Message();
		return $model->find( array(), $conditions, $orders, $limits );
	}

	public static function getReceiver( $messageId )
	{
		$message	= self::getMessage( $messageId );
		return $message['target'];
	}

	public static function getSender( $messageId )
	{
		$message	= self::getMessage( $messageId );
		return $message['source'];
	}

	public static function getStatus( $messageId )
	{
		$message	= self::getMessage( $messageId );
		return $message['status'];
	}

	public static function setStatus( $messageId, $status )
	{
		$model	= new Model_Message( $messageId );
		if( !$model->exists() )
			throw new LogicException( 'message_id_invalid' );
		return $model->update( array( 'status'=> $status ) );
	}

	public static function sendMessage( $senderId, $receiverId, $subject, $body )
	{
		if( empty( $senderId ) )
			throw new CMF_Krypton_Exception_Logic( 'sender_empty' );
		if( empty( $receiverId ) )
			throw new CMF_Krypton_Exception_Logic( 'sender_empty' );
		if( empty( $subject ) )
			throw new CMF_Krypton_Exception_Logic( 'subject_empty' );
		if( empty( $body ) )
			throw new CMF_Krypton_Exception_Logic( 'body_empty' );
		if( !Logic_User::isUser( $senderId ) )
			throw new CMF_Krypton_Exception_Logic( 'sender_invalid' );
		if( !Logic_User::isUser( $receiverId ) )
			throw new CMF_Krypton_Exception_Logic( 'receiver_invalid' );
		$data	= array(
			"source"		=> $senderId,
			"target"		=> $receiverId,
			"subject"		=> $subject,
			"body"			=> $body,
			"timestamp"		=> time(),
			"status"		=> 0
		);
		$model	= new Model_Message();
		return $model->insert( $data );
	}

	public function removeMessage( $messageId, $userId )
	{
		$message	= new Model_Message( $messageId );
		$data		= $message->get( TRUE );
		if( $data['source'] != $userId && $data['target'] != $userId )
			throw new LogicException( 'user_not_owner_of_message' );
		$message->delete();
		return TRUE;
	}
}
?>